package com.hbm.usercenter.base;

import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.hbm.usercenter.R;
import com.hbm.usercenter.base.LoadingPager.LoadedResult;
import com.hbm.usercenter.utils.UIUtils;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;


public abstract class BaseActivity extends ActionBarActivity {

    private LoadingPager mLoadingPager;

    public LoadingPager getLoadingPager() {
        return mLoadingPager;
    }

    // 放置共有的方法
    // 1.activity完全退出
    // 2.退出提示
    // 3.返回当前的activity

    public static LinkedList<BaseActivity> mAllActivities = new LinkedList<BaseActivity>();
    private long mPreClickTime;
    public BaseActivity mCurActivity;

    public BaseActivity getCurActivity() {
        return mCurActivity;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (mLoadingPager == null) {
            mLoadingPager = new LoadingPager(UIUtils.getContext()) {

                @Override
                protected LoadedResult initData() {
                    return BaseActivity.this.initData();
                }

                @Override
                protected View initSuccessView() {
                    return BaseActivity.this.initSuccessView();
                }

            };
        } else {
            ((ViewGroup) mLoadingPager.getParent()).removeView(mLoadingPager);
        }
        setContentView(mLoadingPager);

//        return mLoadingPager;
        mLoadingPager.triggerLoadData();
        init();
//        initView();


        initListener();

    }

    @Override
    protected void onResume() {
        mCurActivity = this;
        super.onResume();
    }
    protected abstract LoadedResult initData();

    protected abstract View initSuccessView();

    public LoadedResult checkState(Object obj) {
        if (obj == null) {
            return LoadedResult.EMPTY;
        }
        if (obj instanceof List) {
            if (((List) obj).size() == 0) {
                return LoadedResult.EMPTY;
            }
        }
        if (obj instanceof Map) {
            if (((Map) obj).size() == 0) {
                return LoadedResult.EMPTY;
            }
        }
        return LoadedResult.SUCCESS;
    }
    protected void init() {

    }

    public Toolbar initToolbar(View view, String title) {
        Toolbar toolbar = (Toolbar)view.findViewById(R.id.toolbar);
        TextView toolbar_title = (TextView)view.findViewById(R.id.toolbar_title);
        setSupportActionBar(toolbar);
        toolbar_title.setText(title);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setDisplayShowTitleEnabled(false);
        }
        return toolbar;
    }


    protected void initListener() {

    }

    /**
     * 完成退出
     */
    public void exit() {
        for (BaseActivity activity : mAllActivities) {
            activity.finish();
        }
    }

//    @Override
//    public void onBackPressed() {
//        if (this instanceof MainActivity) {
//
//            if (System.currentTimeMillis() - mPreClickTime > 2000) {
//                Toast.makeText(UIUtils.getContext(), "再按一次，退出", Toast.LENGTH_SHORT).show();
//                mPreClickTime = System.currentTimeMillis();
//                return;
//            } else {
//                exit();
//            }
//        }
//        super.onBackPressed();
//    }

}
